//
//  SingerDetailViewController.m
//  海角音乐
//
//  Created by dllo on 15/8/14.
//  Copyright (c) 2015年 杨元石. All rights reserved.
//

#import "SingerDetailViewController.h"

@interface SingerDetailViewController ()

@end

@implementation SingerDetailViewController
- (void)viewWillAppear:(BOOL)animated
{
    BarPlayer = [DanLiPlayer shareHandleDanLiPlayer:CGRectZero];
    [self.view addSubview:BarPlayer];
}

- (void)viewDidLoad {
    
    [super viewDidLoad];
    
    CGFloat title_width = SCREEN_WIDTH*0.533333; // 200
    CGFloat title_height = SCREEN_WIDTH*0.08; // 30
    
    self.TitleView = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, title_width, title_height)];
    self.TitleView.textColor = [UIColor whiteColor];
    self.TitleView.textAlignment = NSTextAlignmentCenter;
    self.TitleView.text = self.singerModel.title;;
    self.navigationItem.titleView = self.TitleView;
    
    
    _SingerModelArr = [[NSMutableArray alloc] init];
    
    _SingerDetailView = [[SingerDetailView alloc] initWithFrame:CGRectMake(0, 64, SCREEN_WIDTH, SCREEN_HEIGHT)];
    _SingerDetailView.tableView.dataSource = self;
    _SingerDetailView.tableView.delegate = self;
    _SingerDetailView.backgroundColor = [UIColor clearColor];
    [self.view addSubview:_SingerDetailView];
    [_SingerDetailView release];
    
    HUD = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    HUD.removeFromSuperViewOnHide = YES;
    HUD.labelText = @"loading...";
    _SingerDetailView.tableView.separatorColor = [UIColor clearColor];
    [self.view addSubview:HUD];
    HUD.mode = MBProgressHUDModeIndeterminate;
    
    [self getDataForSingerDetail];
    
    
    
    // Do any additional setup after loading the view.
}


// 获取网络数据
- (void)getDataForSingerDetail
{
    NSString *url_string = [NSString stringWithFormat:@"http://v1.ard.tj.itlily.com/ttpod?a=getnewttpod&id=%@", self.singerModel.singerId];    
    
    [AFN urlString:url_string getAFNdata:^(id responseObject) {
        
        NSArray *dataArr = [responseObject objectForKey:@"data"];
        for (NSDictionary *dic in dataArr) {
            SingerDetaiModel *singerModelDetail = [[SingerDetaiModel alloc] init];
            [singerModelDetail setValuesForKeysWithDictionary:dic];
            [_SingerModelArr addObject:singerModelDetail];
            [singerModelDetail release];
        }
        HUD.hidden = YES;
        _SingerDetailView.tableView.separatorColor = [UIColor lightGrayColor];
        [_SingerDetailView.tableView reloadData];
        
    }];
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    
    return _SingerModelArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellId = @"cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];
    if (cell == NO) {
        
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellId];
        CGFloat singer_X = SCREEN_WIDTH*0.026666; // 10
        CGFloat singer_width = SCREEN_WIDTH*0.2133333; // 80
        UIImageView *singerpic = [[UIImageView alloc] initWithFrame:CGRectMake(singer_X, singer_X, singer_width, singer_width)];
        singerpic.tag = 2001;
        singerpic.backgroundColor = [UIColor redColor];
        [singerpic.layer setMasksToBounds:YES];
        [singerpic.layer setCornerRadius:singer_width/2];
        [cell addSubview:singerpic];
        [singerpic release];
        
        
        CGFloat name_x = SCREEN_WIDTH*0.32; // 120
        CGFloat name_Y = SCREEN_WIDTH*0.08; // 30
        CGFloat name_width = SCREEN_WIDTH*0.53333; // 200
        CGFloat name_height = SCREEN_WIDTH*0.1066667; // 40
        
        UILabel *name = [[UILabel alloc] initWithFrame:CGRectMake(name_x, name_Y, name_width, name_height)];
        name.tag = 2002;
        name.textAlignment = NSTextAlignmentLeft;
        name.font = [UIFont systemFontOfSize:18];
        name.textColor = [UIColor whiteColor];
        [cell addSubview:name];
        [name release];
        
        cell.backgroundColor = [UIColor clearColor];
        
    }
    
    // 数组越界保护
    if (_SingerModelArr.count > indexPath.row) {
        
        SingerDetaiModel *singerDetail = [_SingerModelArr objectAtIndex:indexPath.row];
        UIImageView *singerImage = (UIImageView *)[cell viewWithTag:2001];
        [singerImage sd_setImageWithURL:[NSURL URLWithString:singerDetail.pic_url] placeholderImage:[UIImage imageNamed:@"touxiang.jpg"]];
        
        UILabel *singer_name = (UILabel *)[cell viewWithTag:2002];
        singer_name.text = singerDetail.singer_name;
    }
    // 右边的小箭头
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    return cell;
    
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return SCREEN_WIDTH*0.250666;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    SingerNameViewController *normalVc = [[[SingerNameViewController alloc] init] autorelease];
    SingerDetaiModel *singerDetail = [_SingerModelArr objectAtIndex:indexPath.row];
    normalVc.singerModel = singerDetail;    
    [self.navigationController pushViewController:normalVc animated:YES];
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
